package com.baolai.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baolai.dto.GoodsShelf;
import com.baolai.entity.ShelfGoods;
import com.baolai.mapper.ShelfGoodsMapper;
import com.baolai.result.Result;
import com.baolai.result.ResultUtil;
import com.baolai.service.GoodsRecordService;
import com.baolai.service.ShelfGoodsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShelfGoodsServiceImpl extends ServiceImpl<ShelfGoodsMapper, ShelfGoods> implements ShelfGoodsService {

    @Autowired
    GoodsRecordService goodsRecordService;

    @Autowired
    ShelfGoodsMapper shelfGoodsMapper;

    @Override
    public Result inStock(String goodsId, Integer shelfId, Integer floor, String siteId, Integer number,String account,String name) {
        String site = "";
        site += shelfId < 10 ? "00" + shelfId + "-": shelfId <100 ?"0" + shelfId + "-":shelfId + "-";
        site += floor < 10 ? "0" + floor + "-" : floor + "-";
        site += siteId;
        ShelfGoods shelfGoods = this.getShelfGoods(goodsId,site);
        if(shelfGoods != null){
            shelfGoods.setNumber(shelfGoods.getNumber() + number);
            UpdateWrapper<ShelfGoods> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("goods_id",goodsId).eq("site",site);
            if(this.update(shelfGoods,updateWrapper)){
                if(goodsRecordService.addGoodsRecord("入库",goodsId,site,number,account,name)){
                    return ResultUtil.success("入库成功,记录成功");
                }
                return ResultUtil.success("入库成功,记录失败");
            }
            return ResultUtil.error("入库失败");
        }else{
            ShelfGoods newShelfGoods = new ShelfGoods();
            newShelfGoods.setGoodsId(goodsId);
            newShelfGoods.setSite(site);
            newShelfGoods.setNumber(number);
            if(this.save(newShelfGoods)){
                if(goodsRecordService.addGoodsRecord("入库",goodsId,site,number,account,name)){
                    return ResultUtil.success("入库成功,记录成功");
                }
                return ResultUtil.success("入库成功,记录失败");
            }
            return ResultUtil.error("入库失败");
        }
    }

    @Override
    public ShelfGoods getShelfGoods(String goodsId, String site) {
        QueryWrapper<ShelfGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_id",goodsId).eq("site",site);
        return this.getOne(queryWrapper);
    }

    @Override
    public GoodsShelf getGoodsShelf(String goodsId, String site) {
        return shelfGoodsMapper.getShelfGoods(goodsId,site);
    }

    @Override
    public boolean addShelfGoods(ShelfGoods shelfGoods) {
        return this.save(shelfGoods);
    }

    @Override
    public boolean updateShelfGoods(ShelfGoods shelfGoods) {
        UpdateWrapper<ShelfGoods> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("goods_id",shelfGoods.getGoodsId()).eq("site",shelfGoods.getSite());
        return this.update(shelfGoods,updateWrapper);
    }

    @Override
    public Result outStock(String goodsId, String site, String account, String name,Integer number) {
        ShelfGoods shelfGoods = this.getShelfGoods(goodsId,site);
        if(number > shelfGoods.getNumber()){
            return ResultUtil.error("超出已有数量");
        }
        shelfGoods.setNumber(shelfGoods.getNumber() - number);
        UpdateWrapper<ShelfGoods> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("goods_id",goodsId).eq("site",site);
        if(this.update(shelfGoods,updateWrapper)){
            if(goodsRecordService.addGoodsRecord("出库",goodsId,site,number,account,name)){
                return ResultUtil.success("出库成功,记录成功");
            }
            return ResultUtil.error("记录失败");
        }
        return ResultUtil.error("出库失败");
    }

    @Override
    public JSONObject listShelfGoods(Integer current, Integer size) {
        JSONObject jsonObject = new JSONObject();
        Integer total = this.count();
        Integer pages = (int)Math.ceil(total/size);
        jsonObject.put("total",total);
        jsonObject.put("pages",pages);
        List<GoodsShelf> goodsShelves = shelfGoodsMapper.listShelfGoods((current-1)*size,size);
        jsonObject.put("records",goodsShelves);
        return jsonObject;
    }

    @Override
    public JSONObject queryShelfGoods(String key, Integer current, Integer size) {
        JSONObject jsonObject = new JSONObject();
        Integer total = shelfGoodsMapper.countGoodsShelf(key);
        Integer pages = (int)Math.ceil(total/size);
        jsonObject.put("total",total);
        jsonObject.put("pages",pages);
        List<GoodsShelf> goodsShelves = shelfGoodsMapper.queryGoodsShelf(key,(current-1)*size,size);
        jsonObject.put("records",goodsShelves);
        return jsonObject;
    }

    @Override
    public JSONObject queryShelfGoodsBySite(Integer shelfId, Integer current, Integer size) {
        JSONObject jsonObject = new JSONObject();
        String site = "";
        site += shelfId < 10 ? "00" + shelfId + "-": shelfId <100 ?"0" + shelfId + "-":shelfId + "-";
        QueryWrapper<ShelfGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.likeLeft("site",site);
        Integer total = this.count(queryWrapper);
        Integer pages = (int)Math.ceil(total/size);
        jsonObject.put("total",total);
        jsonObject.put("pages",pages);
        List<GoodsShelf> goodsShelves = shelfGoodsMapper.queryGoodsShelfBySite(site,(current-1)*size,size);
        jsonObject.put("records",goodsShelves);
        return jsonObject;
    }
}
